package com.leaf.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.leaf.user.service.OrderServiceClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

@RestController
public class UserController {

	@Autowired
	private RestTemplate restTemplate;

	@Autowired
	private OrderServiceClient orderServiceClient;

	@RequestMapping("getUser")
	public String getUser() {
		return "user:leaf";
	}

	@RequestMapping("getUserOrder")
	public String getUserOrder() {
//		return restTemplate.getForObject("http://localhost:8011/getOrder", String.class);
		// 通过ribbon请求
		return restTemplate.getForObject("http://CLIENT-ORDER/getOrder", String.class);
	}

	// hystrix通过线程池的方式管理微服务调用，默认调用时长为1秒
	@HystrixCommand(fallbackMethod = "fallbackMethod")
	// 通过feign请求
	@RequestMapping("feignUserOrder")
	public String feignUserOrder(String name) {
		return orderServiceClient.getOrder(name) + name;
	}

	// 接口降级
	public String fallbackMethod(String name) {
		return "降级信息" + name;
	}

	
	@HystrixCommand(fallbackMethod = "fallbackMethod", threadPoolKey = "order", threadPoolProperties = {
			// 并发执行最大线程数
			@HystrixProperty(name = "coreSize", value = "2"),
			// BlockingQueue 最大队列数
			@HystrixProperty(name = "maxQueueSize", value = "1") })
	// 通过feign请求
	@RequestMapping("feignUserOrder1")
	public String feignUserOrder1(String name) {
		return orderServiceClient.getOrder(name) + name;
	}

}
